﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string URI = "http://wap3.qiushibaike.com";
        string pageInfo = Request.QueryString["param"] == null ? string.Empty : Request.QueryString["param"].ToString().Trim();
        URI = URI + pageInfo;

        bodyForm.InnerHtml = Server.HtmlDecode(getQiushi(URI));
    }

    private string getQiushi(string URI)
    {
        WebRequest request = WebRequest.Create(URI);
        WebResponse result = null;
        result = request.GetResponse();
        Stream ReceiveStream = result.GetResponseStream();
        StreamReader sr = new StreamReader(ReceiveStream);
        string resultstring = sr.ReadToEnd();
        StringBuilder responseString = new StringBuilder();

        Regex regContent = new Regex("<div class=\"qiushi\">(?<content>[\\s\\S]+?)</div>");   //匹配糗事内容
        Regex regComment = new Regex("<p class=\"vote\">(?<content>[\\s\\S]+?)</p>");         //匹配评论
        Regex regUserInfo = new Regex("<p class=\"user\">(?<content>[\\s\\S]+?)</p>");        //匹配发布者信息
        Regex regLinks = new Regex("(href=\")(/[^\\s]*)(\")");                                //匹配链接
        Regex regPrevPage = new Regex("<a href=\".*?\">上一页</a>");                          //匹配换页
        Regex regNextPage = new Regex("<a href=\".*?\">下一页</a>");
        Regex regBlankLine = new Regex(@"[\n|\r|\r\n]");                                      //匹配换行
        MatchCollection mcContent = regContent.Matches(resultstring);
        Match mcPrevPage = regPrevPage.Match(resultstring);
        Match mcNextPage = regNextPage.Match(resultstring);
        string prevPage = "<a href=\"?param=" + mcPrevPage.ToString().Replace("<a href=\"", "").Replace("\">上一页</a>", "") + "\">上一页</a>&nbsp;&nbsp;";
        string nextPage = "<a href=\"?param=" + mcNextPage.ToString().Replace("<a href=\"", "").Replace("\">下一页</a>", "") + "\">下一页</a>";

        for (int i = 0; i < mcContent.Count; i++)
        {
            string content = mcContent[i].ToString();
            content = Regex.Replace(content, regComment.ToString(), "", RegexOptions.IgnoreCase);
            content = Regex.Replace(content, regUserInfo.ToString(), "", RegexOptions.IgnoreCase);
            content = Regex.Replace(content, regLinks.ToString(), "href=\"?param=$2\"", RegexOptions.IgnoreCase);
            content = Regex.Replace(content, regBlankLine.ToString(), "", RegexOptions.IgnoreCase);

            responseString.Append(content);
        }

        responseString.Append("<div style=\"text-align:center\">" + prevPage);
        responseString.Append(nextPage + "</div>");

        return responseString.ToString();
    }
}